package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.water.ProductBean
import com.two.screen.water.balance.listener.OnMyKeyCallBack
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.*
import com.two.screen.water.balance.utils.UtilsBigDecimal.toDoubleWithTwoDecimalPlaces
import com.two.screen.water.balance.widget.MutilLineEditText
import java.util.concurrent.TimeUnit


class EditProductInfoDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mEditProductInfoDialogListener: EditProductInfoDialogListener? = null
    private var mTitleTv: TextView? = null
    private var mDialogCancelTv: TextView? = null
    private var mDialogConfirmTv: TextView? = null

    private var mProductNameTv: TextView? = null
    private var mProductSpecsTv: TextView? = null

    private var mProductPriceEt: MutilLineEditText? = null
    private var mProductNumberEt: MutilLineEditText? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_edit_product_info)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mDialogCancelTv = findViewById(R.id.mDialogCancelTv)
        mDialogConfirmTv = findViewById(R.id.mDialogConfirmTv)
        mProductNameTv = findViewById(R.id.item_tv1)
        mProductSpecsTv = findViewById(R.id.item_tv2)
        mProductPriceEt = findViewById(R.id.item_et1)
        mProductNumberEt = findViewById(R.id.item_et2)
        mTitleTv = findViewById(R.id.dialog_title_tv)
        mDialogCancelTv?.setOnClickListener { dismiss() }
    }

    private var mProductBean: ProductBean? = null
    @SuppressLint("CheckResult") fun setNewData(data: ProductBean?) {
        mProductBean = data
        mProductNameTv?.text = mProductBean?.productName
        mProductSpecsTv?.text = ProductBean.getSpecsProductName(mProductBean?.productSpecs)
        mProductPriceEt?.setText(mProductBean?.productUnitPrice)
        mProductNumberEt?.setText("1")


        mProductPriceEt?.let {
            KeyboardUtil.onNumberDecimalKeyDown(mContext, it, object : OnMyKeyCallBack() {
                @SuppressLint("NotifyDataSetChanged")
                override fun onKeyEnter() {
                    mProductBean?.productUnitPrice = String.format(
                        "%.2f", try {
                            it.text.toString().trim().toDouble()
                        } catch (e: Exception) {
                            d("mProductPriceEt->onKeyEnter->error:$e")
                            0.0
                        }
                    )
                    d("mProductPriceEt->onKeyEnter->${mProductBean?.productUnitPrice}")
                }
            })
        }
        mProductNumberEt?.let {
            KeyboardUtil.onNumberDecimalKeyDown(mContext, it, object : OnMyKeyCallBack() {
                @SuppressLint("NotifyDataSetChanged")
                override fun onKeyEnter() {
                    mProductBean?.netWeight = try {
                        it.text.toString().toDoubleWithTwoDecimalPlaces(UtilsBigDecimal.DECIMAL_POINT_NUMBER1)
                    } catch (e: Exception) {
                        d("mProductNumberEt->onKeyEnter->error:$e")
                        0.0
                    }
                    d("mProductNumberEt->onKeyEnter->${mProductBean?.netWeight}")
                }
            })
        }

        mDialogConfirmTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    mProductBean?.productUnitPrice = String.format(
                        "%.2f", try {
                            mProductPriceEt?.text.toString().trim().toDouble()
                        } catch (e: Exception) {
                            d("mProductPriceEt->onKeyEnter->error:$e")
                            0.0001
                        }
                    )
                    mProductBean?.netWeight = try {
                        mProductNumberEt?.text.toString().toDoubleWithTwoDecimalPlaces(UtilsBigDecimal.DECIMAL_POINT_NUMBER1)
                    } catch (e: Exception) {
                        d("mProductNumberEt->onKeyEnter->error:$e")
                        0.0
                    }
                    mEditProductInfoDialogListener?.onConfirm(mProductBean)
                }
        }
    }


    fun setEditProductInfoDialogListener(l: EditProductInfoDialogListener) {
        mEditProductInfoDialogListener = l
    }


    interface EditProductInfoDialogListener {
        fun onConfirm(item: ProductBean?)
    }

    fun d(msg: String) {
        CommUtils.println("EditProductInfoDialog->", msg)
    }

    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
